<template>
	<view>
		<form @submit="submitSub">
			<view class="payment-top flex-row row-column row-center-wrapper">
				<span class="name">我的余额</span>
				<view class="pic">
					￥<span class="pic-font">{{ userAmount.totalPrice || 0 }}</span>
				</view>
			</view>
			<view class="payment">
				<view class="custom-recharge-container"
					style="display: flex; justify-content: center; align-items: center; margin-bottom:30rpx;">
					<view class="custom-recharge"
						style="display: flex; align-items: center; width: 70%; text-align: left;">
						<view class="custom-recharge-title" style="flex-shrink: 0;">充值金额：</view>
						<input class="custom-input" @focus="clearFixedAmountSelection" type="number"
							v-model="customAmount" placeholder="请输入金额" style="border-radius: 10rpx;">
					</view>
				</view>
				<view class="nav flex-row row-around row-middle">
					<view class="item" :class="active==index?'on':''" v-for="(item,index) in navRecharge" :key="index"
						@click="navRecharges(index)">{{item}}</view>
				</view>

				<view class='tip picList' v-if='!active'>
					<view class="pic-box pic-box-color flex-row row-center-wrapper row-column"
						:class="activePic == index ? 'pic-box-color-active' : ''" v-for="(item, index) in picList"
						:key="index" @click="picCharge(index, item)" v-if="item.targetPrice">
						<view class="pic-number-pic">
							{{ item.targetPrice }}<span class="pic-number"> 元</span>
						</view>
						<view class="pic-number">赠送：{{ item.activityPrice }} 元</view>
					</view>

					<view class="tips-box">
						<view class="tips mt-30">注意事项：</view>
						<view class="tips-samll">
							充值后帐户的金额不能提现，可用于商城消费使用
						</view>
						<view class="tips-samll">
							账户充值出现问题可联系商城客服，也可拨打商城客服热线：1380013800
						</view>
					</view>
				</view>
				<button class='but bg-color' formType="submit"> {{active ? '立即转入': '立即充值' }}</button>

			</view>
		</form>
	</view>
</template>

<script>
	import {
		getUserAmount,
	} from '@/api/user.js';
	import {
		getActivity,
		createRecharge
	} from '@/api/recharge.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	export default {
		components: {},
		data() {
			let that = this;
			return {
				now_money: 0,
				// navRecharge: ['账户充值', '佣金转入'],
				navRecharge: ['充值赠送'],
				active: 0,
				number: '',
				userAmount: {},
				placeholder: "0.00",
				picList: [],
				activePic: -1,
				customAmount: '', // 自定义金额绑定
				money: "",
				numberPic: '',
				rechar_id: 0,
			};
		},
		computed: mapGetters(['isLogin', 'uid', 'bgUserType']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserAmount();
						this.getRecharge();
					}
				},
				deep: true
			}
		},
		onLoad(options) {
			if (this.isLogin) {
				this.getUserAmount();
				this.getRecharge();
			} else {
				toLogin();
			}
		},
		methods: {
			// 自定义金额框聚焦事件，清除固定金额选中状态
			clearFixedAmountSelection() {
				this.rechar_id = 0;
				this.activePic = -1; // 取消固定金额的选中
				this.money = ''; // 确保不会用到之前的固定金额
			},
			/**
			 * 选择金额
			 */
			picCharge(idx, item) {
				this.activePic = idx;
				if (item === undefined) {
					this.rechar_id = 0;
					this.numberPic = "";
				} else {
					console.log(item);
					this.money = "";
					this.rechar_id = item.activityId;
					this.numberPic = item.targetPrice;
				}
			},

			/**
			 * 充值额度选择
			 */
			getRecharge() {
				getActivity({
						activityType: this.bgTypeUser ? 'SALE' : 'STORE'
					}).then(res => {
						this.picList = res.value;
						if (this.picList[0]) {
							this.rechar_id = this.picList[0].activityId;
							this.numberPic = this.picList[0].targetPrice;
						}
					})
					.catch(res => {
						this.$util.Tips({
							title: res
						})
					});
			},

			navRecharges: function(index) {
				this.active = index;
			},
			/**
			 * 获取用户信息
			 */
			getUserAmount: function() {
				let that = this;
				getUserAmount({
					userInfoId: this.uid
				}).then(res => {
					that.$set(that, 'userAmount', res.value);
				})
			},
			/*
			 * 用户充值
			 */
			submitSub: function(e) {
				// let that = this  e.detail.value.number;
				let value = e.detail.value.number;
				let that = this;
				// 自定义金额优先
				let money = parseFloat(this.customAmount); // 从自定义金额输入框获取值
				console.log("money=" + money + "-and-Number.isNaN(money)" + Number.isNaN(money));
				if (Number.isNaN(money) || money <= 0) {
					// 如果自定义金额无效，则检查是否选择了固定充值金额
					console.log("this.activePic=" + this.activePic);
					if (this.activePic !== -1) {
						money = parseFloat(this.money); // 使用固定选项金额
					} else {
						// 两者都无效，提示用户
						return that.$util.Tips({
							title: '请输入有效的充值金额'
						});
					}
				}
				// 转入余额
				if (!that.active) {
					uni.showLoading({
						title: '正在支付',
					})
					// #ifdef MP || APP-PLUS
					// let money = parseFloat(this.money);
					console.log("this.rechar_id=" + this.rechar_id);
					if (this.rechar_id == 0) {
						if (Number.isNaN(money)) {
							return that.$util.Tips({
								title: '充值金额必须为数字'
							});
						}
						if (money <= 0) {
							return that.$util.Tips({
								title: '充值金额不能为0'
							});
						}
					} else {
						money = this.numberPic;
					}
					console.log("value=" + value);
					createRecharge({
						rechargePrice: money,
						userInfoId: this.uid,
						activityId: this.activePic !== -1 ? this.rechar_id : ''
					}).then(res => {
						uni.hideLoading();
						let jsConfig = res.value.param;
						uni.requestPayment({
							// #ifdef MP
							timeStamp: jsConfig.timeStamp,
							nonceStr: jsConfig.nonceStr,
							package: jsConfig.package,
							signType: jsConfig.signType,
							paySign: jsConfig.paySign,
							// #endif
							// #ifdef APP-PLUS
							provider: 'wxpay',
							orderInfo: jsConfig,
							// #endif
							success: function(res) {
								that.$set(that, 'userAmount.totalPrice', that.$util.$h.Add(value,
									that.userAmount.totalPrice));
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 5,
									url: '/pages/users/user_money/index'
								});
							},
							fail: function() {
								return that.$util.Tips({
									title: '支付失败'
								});
							},
							complete: function(res) {
								if (res.errMsg == 'requestPayment:cancel') return that.$util.Tips({
									title: '取消支付'
								});
							}
						})
					}).catch(err => {
						uni.hideLoading();
						return that.$util.Tips({
							title: err
						})
					});
					// #endif
				}
			},
			// 选择充值选项
			picCharge(index, item) {
				this.activePic = index;
				this.customAmount = ''; // 选择固定金额时清空自定义金额
				this.money = item.targetPrice;
				this.rechar_id = item.activityId || 0; // 关联活动ID
			},
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}

	.custom-recharge {
		margin-top: 20px;
	}

	.custom-input {
		width: 100%;
		padding: 10px;
		font-size: 14px;
		border: 1px solid #ddd;
		border-radius: 4px;
	}


	.bgcolor {
		background-color: #3CBB45;
	}

	.payment {
		position: relative;
		top: -60rpx;
		width: 100%;
		background-color: #fff;
		border-radius: 10rpx;
		padding-top: 25rpx;
		border-top-right-radius: 39rpx;
		border-top-left-radius: 39rpx;
	}

	.payment .nav {
		height: 75rpx;
		line-height: 75rpx;
		padding: 0 100rpx;
	}

	.payment .nav .item {
		font-size: 30rpx;
		color: #333;
	}

	.payment .nav .item.on {
		font-weight: bold;
		border-bottom: 4rpx solid #3CBB45;
	}

	.payment .input {
		display: flex;
		align-items: center;
		justify-content: center;
		border-bottom: 1px dashed #dddddd;
		margin: 60rpx auto 0 auto;
		padding-bottom: 20rpx;
		font-size: 56rpx;
		color: #333333;
		flex-wrap: nowrap;

	}

	.payment .input text {
		padding-left: 106rpx;
	}

	.payment .input input {
		padding-right: 106rpx;
		width: 300rpx;
		height: 94rpx;
		text-align: center;
		font-size: 70rpx;
	}

	.payment .placeholder {
		color: #d0d0d0;
		height: 100%;
		line-height: 94rpx;
	}

	.payment .tip {
		font-size: 26rpx;
		color: #888888;
		padding: 0 30rpx;
		margin-top: 25rpx;
	}

	.payment .but {
		color: #fff;
		font-size: 30rpx;
		width: 700rpx;
		height: 86rpx;
		border-radius: 50rpx;
		margin: 46rpx auto 0 auto;
		line-height: 86rpx;
	}

	.payment-top {
		width: 100%;
		height: 350rpx;
		background-color: #3CBB45;

		.name {
			font-size: 26rpx;
			color: rgba(255, 255, 255, 0.8);
			margin-top: -38rpx;
			margin-bottom: 30rpx;
		}

		.pic {
			font-size: 32rpx;
			color: #fff;
		}

		.pic-font {
			font-size: 78rpx;
			color: #fff;
		}
	}

	.picList {
		display: flex;
		flex-wrap: wrap;
		margin: 30rpx 0;

		.pic-box {
			width: 32%;
			height: auto;
			border-radius: 20rpx;
			margin-top: 21rpx;
			padding: 20rpx 0;
			margin-right: 12rpx;

			&:nth-child(3n) {
				margin-right: 0;
			}
		}

		.pic-box-color {
			background-color: #f4f4f4;
			color: #656565;
		}

		.pic-number {
			font-size: 22rpx;
		}

		.pic-number-pic {
			font-size: 38rpx;
			margin-right: 10rpx;
			text-align: center;
		}

		.pic-box-color-active {
			background-color: #3CBB45 !important;
			color: #fff !important;
		}
	}

	.tips-box {
		.tips {
			font-size: 28rpx;
			color: #333333;
			font-weight: 800;
			margin-bottom: 14rpx;
			margin-top: 20rpx;
		}

		.tips-samll {
			font-size: 24rpx;
			color: #333333;
			margin-bottom: 14rpx;
		}

		.tip-box {
			margin-top: 30rpx;
		}
	}

	.tips-title {
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #333;
	}
</style>